<?php

Globals::requireClass('Controller');
Globals::requireTable('Province');
Globals::requireTable('City');
Globals::requireTable('Yard');
Globals::requireTable('Box');

class AreaController extends Controller {

    protected $ProvinceTable;
    protected $CityTable;
    protected $YardTable;
    protected $BoxTable;
    public static $defaultConfig = array(
        'viewEnabled' => true,
        'layoutEnabled' => true,
        'title' => null
    );

    public function __construct($config = null) {
        parent::__construct($config);
        $this->ProvinceTable = new ProvinceTable($config);
        $this->CityTable = new CityTable($config);
        $this->YardTable = new YardTable($config);
        $this->BoxTable = new BoxTable($config);
    }

    public function indexAction() {
        $this->config['layoutEnabled'] = false;
    }

    public function arealistAction() {

        if (!in_array(1, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $uid = $_SESSION['userinfo']['id'];

        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $name = $this->getParam('search_name');
        $status = $this->getParam('search_type');
        $where = "user_id = '{$uid}'";
        if (!empty($name)) {
            $where .= " and name = '{$name}'";
            $this->view->name = $name;
        }

        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
            $this->view->status = 3;
        }
        $count = $this->ProvinceTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->ProvinceTable->listPage($where, "id desc", $pageSize, $pageId);
        //$re = $this->ProvinceTable->listall($where);
        $this->view->data = $data;

        $this->config['layoutEnabled'] = false;
    }

    public function doareaaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $province_name = $this->getParam('province_name');
        $province_code = $this->getParam('province_code');

        $time = date("Y-m-d H:i:s", time());

        $where = "user_id = '{$uid}'";
        $re = $this->ProvinceTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $province_name) {
                echo 0;
                exit;
            }
        }


        $data = array(
            'user_id' => $uid,
            'code' => $province_code,
            'name' => $province_name,
            'createtime' => $time,
        );
        $re = $this->ProvinceTable->add($data);
        echo $re;


        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doareaupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $province_name = $this->getParam('province_name');
        $province_code = $this->getParam('province_code');
        $id = $this->getParam('id');
        $where = "user_id = '{$uid}'";
        $re = $this->ProvinceTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $province_name) {
                echo 0;
                exit;
            }
        }

        $where2 = "id =" . $id;
        $data = array(
            'code' => $province_code,
            'name' => $province_name,
        );
        $re = $this->ProvinceTable->update($data, $where2);
        echo $re;



        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doareadelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->ProvinceTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function citylistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        if (!in_array(2, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $province_id = $this->getParam('province_id');
        $id = $this->getParam('id');
        $status = $this->getParam('search_type');
        $where = "user_id = '{$uid}'";
        $where2 = "user_id = '{$uid}'";
        if (!empty($province_id)) {
            $where .= " and province_id = '{$province_id}'";
            $this->view->province_id = $province_id;
        }

        if (!empty($id)) {
            $where .= " and id = '{$id}'";
            $this->view->id = $id;
        }

        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $where2 .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $where2 .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
            $where2 .= " and status > 0";
        }
        $province = $this->ProvinceTable->listall($where2);
        $this->view->province = $province;
        $count = $this->CityTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $re = $this->CityTable->listPage($where, "id desc", $pageSize, $pageId);
        foreach ($re as $k => $v) {
            $re[$k]['province_name'] = '';
            foreach ($province as $kk => $vv) {
                if ($v['province_id'] == $vv['id']) {
                    $re[$k]['province_name'] = $province[$kk]['name'];
                }
            }
        }

        $this->view->data = $re;



        $this->config['layoutEnabled'] = false;
    }

    public function docityaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $city_name = $this->getParam('city_name');
        $city_code = $this->getParam('city_code');
        $province_id = $this->getParam('province_id');
        $is_yard = $this->getParam('is_yard');
        $time = date("Y-m-d H:i:s", time());

        $data = array(
            'user_id' => $uid,
            'code' => $city_code,
            'name' => $city_name,
            'province_id' => $province_id,
            'is_yard' => $is_yard,
            'createtime' => $time,
        );
        $re = $this->CityTable->add($data);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function docityupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $city_name = $this->getParam('city_name');
        $city_code = $this->getParam('city_code');
        $province_id = $this->getParam('province_id');
        $is_yard = $this->getParam('is_yard');
        $id = $this->getParam('id');


        $where = "user_id = '{$uid}' and id != '{$id}'";
        $re = $this->CityTable->listall($where);
        foreach ($re as $k => $v) {
            if ($v['name'] == $city_name) {
                echo 0;
                exit;
            }
        }

        $where2 = "id =" . $id;
        $data = array(
            'code' => $city_code,
            'name' => $city_name,
            'province_id' => $province_id,
            'is_yard' => $is_yard,
        );
        $re = $this->CityTable->update($data, $where2);
        echo $re;



        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function docitydelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }

        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->CityTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function yardlistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        if (!in_array(3, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $city = $this->getParam('city');
        $id = $this->getParam('id');
        $status = $this->getParam('search_type');
        $where = "user_id = '{$uid}'";
        $where2 = "user_id = '{$uid}'";
        if (!empty($city)) {
            $where .= " and city = '{$city}'";
            $this->view->city_id = $city;
        }

        if (!empty($id)) {
            $where .= " and id = '{$id}'";
            $this->view->id = $id;
        }

        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $where2 .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $where2 .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
            $where2 .= " and status > 0";
        }

        $city = $this->CityTable->listall($where2);
        $this->view->city = $city;
        $count = $this->YardTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $re = $this->YardTable->listPage($where, "id desc", $pageSize, $pageId);
        // $re = $this->YardTable->listall($where);

        foreach ($re as $k => $v) {
            $re[$k]['city_name'] = '';
            foreach ($city as $kk => $vv) {
                if ($v['city'] == $vv['id']) {
                    $re[$k]['city_name'] = $city[$kk]['name'];
                }
            }
        }

        $this->view->data = $re;

        $this->config['layoutEnabled'] = false;
    }

    public function doyardaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $province_name = $this->getParam('province_name');
        $province_code = $this->getParam('province_code');
        $contact = $this->getParam('contact');
        $telephone = $this->getParam('telephone');
        $fax = $this->getParam('fax');
        $city = $this->getParam('city');
        $shipping_bussiness = $this->getParam('shipping_bussiness');
        $gate_money = $this->getParam('gate_money');
        $address = $this->getParam('address');
        $time = date("Y-m-d H:i:s", time());

        $data = array(
            'user_id' => $uid,
            'code' => $province_code,
            'name' => $province_name,
            'city' => $city,
            'contact' => $contact,
            'telephone' => $telephone,
            'fax' => $fax,
            'shipping_bussiness' => $shipping_bussiness,
            'gate_money' => $gate_money,
            'address' => $address,
            'createtime' => $time,
        );
        $re = $this->YardTable->add($data);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doyardupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $province_name = $this->getParam('province_name');
        $province_code = $this->getParam('province_code');
        $contact = $this->getParam('contact');
        $telephone = $this->getParam('telephone');
        $fax = $this->getParam('fax');
        $city = $this->getParam('city');
        $shipping_bussiness = $this->getParam('shipping_bussiness');
        $gate_money = $this->getParam('gate_money');
        $address = $this->getParam('address');
        $id = $this->getParam('id');


//            $where = "user_id = '{$uid}'";
//            $re = $this->YardTable->listall($where);
//            foreach($re as $k => $v){
//                if($v['name'] == $province_name){
//                    echo 0;exit;
//                }
//            }

        $where2 = "id =" . $id;

        $data = array(
            'code' => $province_code,
            'name' => $province_name,
            'city' => $city,
            'contact' => $contact,
            'telephone' => $telephone,
            'fax' => $fax,
            'shipping_bussiness' => $shipping_bussiness,
            'gate_money' => $gate_money,
            'address' => $address,
        );
        $re = $this->YardTable->update($data, $where2);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doyarddelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->YardTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function boxlistAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        if (!in_array(4, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $code = $this->getParam('code');
        $status = $this->getParam('search_type');
        $where = "user_id = '{$uid}'";

        if (!empty($code)) {
            $where .= " and code = '{$code}'";
            $this->view->code = $code;
        }

        if (!empty($status)) {
            if ($status == 3) {
                $where .= " and status > 0";
                $this->view->status = $status;
            } else {
                $where .= " and status = '{$status}'";
                $this->view->status = $status;
            }
        } else {
            $where .= " and status > 0";
        }

        //$re = $this->BoxTable->listall($where);
        $count = $this->BoxTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->BoxTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->data = $data;
        $this->config['layoutEnabled'] = false;
    }

    public function doboxaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $describe = $this->getParam('describe');
        $province_code = $this->getParam('province_code');
        $rank = $this->getParam('rank');
        $weight = $this->getParam('weight');

        $time = date("Y-m-d H:i:s", time());

        $data = array(
            '`user_id`' => $uid,
            '`code`' => $province_code,
            '`describe`' => $describe,
            '`rank`' => $rank,
            '`weight`' => $weight,
            '`createtime`' => $time,
        );
        $re = $this->BoxTable->add($data);
        echo $re;



        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doboxupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $describe = $this->getParam('describe');
        $province_code = $this->getParam('province_code');
        $rank = $this->getParam('rank');
        $weight = $this->getParam('weight');
        $id = $this->getParam('id');

        $where = "id = " . $id;
        $data = array(
            '`code`' => $province_code,
            '`describe`' => $describe,
            '`rank`' => $rank,
            '`weight`' => $weight,
        );
        $re = $this->BoxTable->update($data, $where);
        echo $re;



        $this->config['layoutEnabled'] = false;
        exit;
    }

    public function doboxdelAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        $id = $this->getParam('id');
        $type = $this->getParam('type');

        $where = "id = " . $id;
        $data = array(
            'status' => $type,
        );
        $re = $this->BoxTable->update($data, $where);
        echo $re;

        $this->config['layoutEnabled'] = false;
        exit;
    }

    protected function out() {
        $this->layout->nav = 'index';
        parent::out();
    }

}

Config::extend('AreaController', 'Controller');


